package com.ljkj.blockpay.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Layout;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.kongzue.baseokhttp.util.JsonMap;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseActivity;
import com.ljkj.blockpay.databinding.ActivityTransferAccountsBinding;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.presenter.RechargePresenter;
import com.ljkj.blockpay.presenter.inter.RechargeInter;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;
import com.ljkj.blockpay.widget.VerifyCodeView;
import com.luck.picture.lib.tools.ToastUtils;
import com.yxing.ScanCodeConfig;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;

/**
 * 转账具体详情页
 */
public class TransferAccountsActivity extends BaseActivity<ActivityTransferAccountsBinding, RechargePresenter> implements RechargeInter.RechargeInterView, ClickListener {

    public static final  String TAG_BIZTOKEN = "TradingParticularsActivity.tag_biztoken";
    public static final  String TAG_ACCOUNT = "TradingParticularsActivity.tag_account";
    public static final  String TAG_HEADURL = "TradingParticularsActivity.tag_headurl";
    private static final int EDIT_OK = 123;
    private String biztoken;
    private String account;
    private String headurl;
    private String serviceAmount;
    private String originAmount;
    private String remark;
    private CommonDialog mConfirmDialog;
    private CommonDialog passwordDialog;
    private CommonDialog dismissDialog;
    private  String orderId;
    private String balance;
    private BigDecimal bigDecimal;
    private String origin;
    private String currencyName;
    private String currencyImg;

    @Override
    public int initContentView() {
        return R.layout.activity_transfer_accounts;
    }

    @Override
    protected RechargePresenter createPresenter() {
        return new RechargePresenter(this,this);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        binding.setOnClick(this::onClick);
        binding.tvTitle.setTitleContent(R.string.hometransfer);
        binding.tvTitle.setLeftBackFinish(this);


    }

    @Override
    protected void initData() {
        super.initData();
        biztoken = getIntent().getStringExtra(TAG_BIZTOKEN);
        account = getIntent().getStringExtra(TAG_ACCOUNT);
        headurl = getIntent().getStringExtra(TAG_HEADURL);
        Glide.with(baseContext).load(headurl).into(binding.ivIcon);
        binding.tvName.setText(account);
        getPresenter().getNetAccuracy(2);
        binding.etRemark.setText(remark);
        getPresenter().preConfirm(biztoken,bigDecimal,2,"","",currencyName,"",remark);
        origin = binding.etNum.getText().toString();
        binding.etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!editable.toString().isEmpty()){
                    binding.btnNext.setBackground(getResources().getDrawable(R.drawable.btnbg));
                }
            }
        });

    }


    //监听输入框
    public void getEditText() {
        origin = binding.etNum.getText().toString();
        if (!origin.isEmpty()) {
            bigDecimal = new BigDecimal(origin);
            getPresenter().preConfirm(biztoken,bigDecimal,2,"","",currencyName,"",remark);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_next:
                mConfirmDialog = CommonDialog.Builder.with(this).setContentView(R.layout.confirmorderdialog_layout)
                        .forGravity(Gravity.BOTTOM)
                        .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                passwordDialog = CommonDialog.Builder.with(TransferAccountsActivity.this)
                                        .setContentView(R.layout.codeedit_layout).forGravity(Gravity.BOTTOM).setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {

                                                dismissDialog = CommonDialog.Builder.with(TransferAccountsActivity.this).setContentView(R.layout.dropbusiness_layout)
                                                        .forGravity(Gravity.CENTER)
                                                        .setText(R.id.btn_ok,R.string.continuetrade)
                                                        .setOnClickListener(R.id.btn_cancel, new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View view) {
                                                                ToggleToActivity.toMainActivity(TransferAccountsActivity.this);
                                                            }
                                                        })
                                                        .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View view) {
                                                                dismissDialog.dismiss();
                                                            }
                                                        }).setCancelable(false).setOnKeyListener((dialog, keyCode, event) -> false).create();
                                                dismissDialog.show();
                                            }
                                        })
                                        .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                //交易密码
                                                View inflate = LayoutInflater.from(baseContext).inflate(R.layout.dropbusiness_layout, null, false);
                                                VerifyCodeView verify_code = inflate.findViewById(R.id.verify_code_view);
                                                Button btn = inflate.findViewById(R.id.btn_ok);
                                                    btn.setSelected(true);
                                                    getPresenter().createOrder(2,getPresenter().preOrderId,"123456");

                                            }
                                        }).setCancelable(true).fullWidth().setOnKeyListener((dialog, keyCode, event) -> false).create();
                                passwordDialog.show();

                            }
                        })
                        .setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mConfirmDialog.dismiss();
                            }
                        })
                        .fullWidth().setCancelable(true).setOnKeyListener((dialog, keyCode, event) -> false).create();

                origin = binding.etNum.getText().toString();
                if (!origin.isEmpty()) {
                    bigDecimal = new BigDecimal(origin);
                }
                TextView originalamount = mConfirmDialog.findViewById(R.id.tv_originalamount);
                TextView serviceAmount = mConfirmDialog.findViewById(R.id.tv_ServiceAmount);
                TextView targetAccount = mConfirmDialog.findViewById(R.id.tv_targetAccount);
                TextView originCurrency = mConfirmDialog.findViewById(R.id.tv_originCurrency);
                TextView num = mConfirmDialog.findViewById(R.id.tv_number);
                TextView numunit = mConfirmDialog.findViewById(R.id.tv_dialogunit);

                TextView remarks = mConfirmDialog.findViewById(R.id.tv_remark);
                TextView eamount = mConfirmDialog.findViewById(R.id.tv_eAmount);
                num.setText(bigDecimal+"");
                numunit.setText(currencyName);
                originalamount.setText(bigDecimal+currencyName);
                eamount.setVisibility(View.GONE);
                serviceAmount.setVisibility(View.GONE);
                targetAccount.setText(account);
                originCurrency.setText(currencyName);
                remarks.setText(remark);
                mConfirmDialog.show();
                break;

            case R.id.tv_unit:
                ToggleToActivity.toSelectCurrencyActivity(this,10);
                break;
        }

    }

    /** 输入框小数的位数*/
    private static final int DECIMAL_DIGITS = 1;

    /**
     *  设置小数位数控制
     */
    InputFilter lengthfilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            // 删除等特殊字符，直接返回
            if ("".equals(source.toString())) {
                return null;
            }
            String dValue = dest.toString();
            String[] splitArray = dValue.split("\\.");
            if (splitArray.length > 1) {
                String dotValue = splitArray[1];
                int diff = dotValue.length() + 1 - DECIMAL_DIGITS;
                if (diff > 0) {
                    return source.subSequence(start, end - diff);
                }
            }
            return null;
        }
    };
    @Override
    public void showNetAccuracy(List<RechargeModel> modelList) {
        currencyName = modelList.get(0).getCurrencyName();
        currencyImg = modelList.get(0).getCurrencyImg();
        binding.tvUnit.setText(currencyName);
        Glide.with(baseContext).load(currencyImg).into(binding.ivIcons);
        getPresenter().SearchCurrencyBalance(currencyName,2);

    }

    @Override
    public void showrecharge(JsonMap jsonMap) {

    }

    @Override
    public void createOrderSuccess() {

    }

    @Override
    public void SearchCurrencySuccess(JsonMap jsondata) {
        balance = jsondata.getString("balance");
        binding.tvUsable.setText(balance);

    }

    @Override
    public void getOrderDetails(JsonMap jsonMap) {
        getPresenter().preOrderId = jsonMap.getString("preOrderId");
        serviceAmount = jsonMap.getString("originServiceAmount");
        originAmount = jsonMap.getString("originAmount");
        remark = jsonMap.getString("remark");
        binding.tvParitienum.setText(serviceAmount);
        binding.etNum.setText(originAmount);
    }

    @Override
    public void getOrderDetails2(JsonMap jsonMap) {
        orderId = jsonMap.getString("orderId");
        ToggleToActivity.toTradingParticularActivity(TransferAccountsActivity.this,2,orderId);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != 100) {
            return;
        }
        if (data.getExtras() != null) {
            Bundle bundle = data.getExtras();
            JsonMap jsonMap = JsonMap.parse(bundle.getString("jsonMap"));
            switch (requestCode) {
                case 10:
                    currencyName = jsonMap.getString("currencyName");
                    currencyImg = jsonMap.getString("currencyImg");
                    binding.tvUnit.setText(currencyName);
                    Glide.with(baseContext).load(currencyImg).into(binding.ivIcons);
                    getPresenter().SearchCurrencyBalance(currencyName,2);

                    break;
                default:
                    break;
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}